package com.learning.optimize.jdk.regex;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AdvancedCareerMatcher {
    private Pattern extendedPattern;

    public AdvancedCareerMatcher() {
        // 支持更多前缀的扩展模式
        extendedPattern = Pattern.compile("\\[(zhiyecc\\d+|code|career_code|job_code)\\](的职业代码|是几类职业|职业代码是多少)");
    }

    /**
     * 验证并提取结构化信息
     */
    public Map<String, Object> validateAndExtract(String text) {
        Matcher matcher = extendedPattern.matcher(text);
        if (!matcher.matches()) {
            return null;
        }

        String prefix = matcher.group(1);
        String questionType = matcher.group(2);

        // 根据问题类型提供不同的响应
        Map<String, String> responseMap = new HashMap<>();
        responseMap.put("的职业代码", "获取" + prefix + "的职业编码");
        responseMap.put("是几类职业", "查询" + prefix + "的职业分类");
        responseMap.put("职业代码是多少", "查找" + prefix + "的职业代码值");

        Map<String, Object> result = new HashMap<>();
        result.put("prefix", prefix);
        result.put("question_type", questionType);
        result.put("suggested_response", responseMap.getOrDefault(questionType, "未知查询类型"));
        result.put("is_valid", true);

        return result;
    }

    /**
     * 批量处理多个测试用例
     */
    public List<Map<String, Object>> processBatch(List<String> testCases) {
        List<Map<String, Object>> results = new ArrayList<>();
        for (String testCase : testCases) {
            Map<String, Object> result = validateAndExtract(testCase);
            if (result != null) {
                results.add(result);
            }
        }
        return results;
    }

    public static void main(String[] args) {
        AdvancedCareerMatcher advancedMatcher = new AdvancedCareerMatcher();

        List<String> testCases = Arrays.asList(
                "[zhiyecc1]的职业代码",
                "[zhiyecc2]是几类职业",
                "[code]职业代码是多少",
                "[career_code]的职业代码",
                "[zhiyecc3]的职业代码",  // 支持zhiyecc+数字的模式
                "[invalid]的职业代码"    // 这个不会匹配
        );

        System.out.println("=== 进阶匹配结果 ===");
        for (String testCase : testCases) {
            Map<String, Object> result = advancedMatcher.validateAndExtract(testCase);
            if (result != null) {
                System.out.println("输入: " + testCase);
                System.out.println("解析: " + result + "\n");
            } else {
                System.out.println("输入: " + testCase + " - 不匹配\n");
            }
        }

        System.out.println("=== 批量处理结果 ===");
        List<Map<String, Object>> batchResults = advancedMatcher.processBatch(testCases);
        for (Map<String, Object> result : batchResults) {
            System.out.println(result);
        }
    }
}